﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Runtime.InteropServices;
    using System.Text;

    public class OrderLog
    {
        public int Add(Chain.Model.OrderLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into OrderLog(");
            builder.Append("OrderAccount,OrderType,OrderMemID,OrderTotalMoney,OrderDiscountMoney,OrderPayCard,OrderPayCash,OrderPayCoupon,OrderPoint,OrderRemark,OrderPayType,OrderShopID,OrderIsCard,OrderIsCash,OrderIsBink,OrderPayBink,OrderUserID,OldAccount,OrderCardBalance,OrderCardPoint)");
            builder.Append(" values (");
            builder.Append("@OrderAccount,@OrderType,@OrderMemID,@OrderTotalMoney,@OrderDiscountMoney,@OrderPayCard,@OrderPayCash,@OrderPayCoupon,@OrderPoint,@OrderRemark,@OrderPayType,@OrderShopID,@OrderIsCard,@OrderIsCash,@OrderIsBink,@OrderPayBink,@OrderUserID,@OldAccount,@OrderCardBalance,@OrderCardPoint)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@OrderAccount", SqlDbType.VarChar, 50), new SqlParameter("@OrderType", SqlDbType.TinyInt, 1), new SqlParameter("@OrderMemID", SqlDbType.Int, 4), new SqlParameter("@OrderTotalMoney", SqlDbType.Money, 8), new SqlParameter("@OrderDiscountMoney", SqlDbType.Money, 8), new SqlParameter("@OrderPayCard", SqlDbType.Money, 8), new SqlParameter("@OrderPayCash", SqlDbType.Money, 8), new SqlParameter("@OrderPayCoupon", SqlDbType.Money, 8), new SqlParameter("@OrderPoint", SqlDbType.Int, 4), new SqlParameter("@OrderRemark", SqlDbType.NVarChar, 500), new SqlParameter("@OrderPayType", SqlDbType.TinyInt, 1), new SqlParameter("@OrderShopID", SqlDbType.Int, 4), new SqlParameter("@OrderIsCard", SqlDbType.Bit), new SqlParameter("@OrderIsCash", SqlDbType.Bit), new SqlParameter("@OrderIsBink", SqlDbType.Bit), new SqlParameter("@OrderPayBink", SqlDbType.Money, 8), 
                new SqlParameter("@OrderUserID", SqlDbType.Int, 4), new SqlParameter("@OldAccount", SqlDbType.NVarChar, 50), new SqlParameter("@OrderCardBalance", SqlDbType.Money, 8), new SqlParameter("@OrderCardPoint", SqlDbType.Int, 4)
             };
            cmdParms[0].Value = model.OrderAccount;
            cmdParms[1].Value = model.OrderType;
            cmdParms[2].Value = model.OrderMemID;
            cmdParms[3].Value = model.OrderTotalMoney;
            cmdParms[4].Value = model.OrderDiscountMoney;
            cmdParms[5].Value = model.OrderPayCard;
            cmdParms[6].Value = model.OrderPayCash;
            cmdParms[7].Value = model.OrderPayCoupon;
            cmdParms[8].Value = model.OrderPoint;
            cmdParms[9].Value = model.OrderRemark;
            cmdParms[10].Value = model.OrderPayType;
            cmdParms[11].Value = model.OrderShopID;
            cmdParms[12].Value = model.OrderIsCard;
            cmdParms[13].Value = model.OrderIsCash;
            cmdParms[14].Value = model.OrderIsBink;
            cmdParms[15].Value = model.OrderPayBink;
            cmdParms[0x10].Value = model.OrderUserID;
            cmdParms[0x11].Value = model.OldAccount;
            cmdParms[0x12].Value = model.OrderCardBalance;
            cmdParms[0x13].Value = model.OrderCardPoint;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int OrderID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrderLog ");
            builder.Append(" where OrderID=@OrderID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderID", SqlDbType.Int, 4) };
            cmdParms[0].Value = OrderID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string OrderIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from OrderLog ");
            builder.Append(" where OrderID in (" + OrderIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int OrderID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from OrderLog");
            builder.Append(" where OrderID=@OrderID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderID", SqlDbType.Int, 4) };
            cmdParms[0].Value = OrderID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public bool ExistsOrderAccount(string OrderAccount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from OrderLog");
            builder.Append(" where OrderAccount=@OrderAccount");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderAccount", SqlDbType.VarChar, 50) };
            cmdParms[0].Value = OrderAccount;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public decimal GetAllMoney(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SUM(OrderTotalMoney) FROM OrderLog,Mem");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0M;
            }
            return Convert.ToDecimal(single);
        }

        public decimal GetCoupon(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SUM(OrderPayCoupon) FROM OrderLog,Mem ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0M;
            }
            return Convert.ToDecimal(single);
        }

        public decimal GetDiscount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SUM(OrderDiscountMoney) FROM OrderLog,Mem ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0M;
            }
            return Convert.ToDecimal(single);
        }

        public decimal GetDiscountMoney(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SUM(OrderDiscountMoney) FROM OrderLog,Mem,MemLevel,SysShop,SysUser ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0M;
            }
            return Convert.ToDecimal(single);
        }

        public DataSet GetEmptyBillsList(int PageSize, int PageIndex, out int resCount, string strSql, params string[] strWhere)
        {
            string[] column = new string[] { "*" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = GetEmptyBillsTable(column, condition, "OrderID", strSql, false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public static DataSet GetEmptyBillsTable(string[] column, string[] condition, string IndexColumn, string strSql, bool IsAsc, int PageSize, int Page, int RecordCount)
        {
            string str = "";
            string inputSql = "";
            for (int i = 0; i < column.Length; i++)
            {
                str = str + column[i];
                if (i < (column.Length - 1))
                {
                    str = str + ",";
                }
            }
            if ((condition != null) && (condition.Length > 0))
            {
                for (int j = 0; j < condition.Length; j++)
                {
                    if (!(condition[j] == ""))
                    {
                        inputSql = inputSql + condition[j];
                        if (j < (condition.Length - 1))
                        {
                            inputSql = inputSql + " and ";
                        }
                    }
                }
            }
            inputSql = DbHelperSQL.ProcessSql(inputSql);
            SqlParameter[] parameter = new SqlParameter[] { new SqlParameter("@ReturnFields", SqlDbType.VarChar, 500), new SqlParameter("@OrderFields", SqlDbType.VarChar, 0xff), new SqlParameter("@PageSize", SqlDbType.Int, 4), new SqlParameter("@PageIndex", SqlDbType.Int, 4), new SqlParameter("@PKField", SqlDbType.VarChar, 0xff), new SqlParameter("@IsDesc", SqlDbType.Bit), new SqlParameter("@Where", SqlDbType.VarChar, 0x3e8), new SqlParameter("@levelID", SqlDbType.VarChar, 50) };
            parameter[0].Value = str;
            parameter[1].Value = IndexColumn;
            parameter[2].Value = PageSize;
            parameter[3].Value = Page;
            parameter[4].Value = IndexColumn;
            parameter[5].Value = IsAsc ? 0 : 1;
            parameter[6].Value = inputSql;
            parameter[7].Value = strSql;
            return DbHelperSQL.ExecuteProcedure("CP_EmptyBills", parameter);
        }

        public DataSet GetGoodsExpense(DateTime starttime, DateTime endtime, string GoodsCode, string strwhere)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@StartTime", SqlDbType.DateTime), new SqlParameter("@EndTime", SqlDbType.DateTime), new SqlParameter("@GoodsCode", SqlDbType.NVarChar, 50), new SqlParameter("@strWhere", SqlDbType.NVarChar, 0x7d0) };
            parameters[0].Value = starttime;
            parameters[1].Value = endtime;
            parameters[2].Value = GoodsCode;
            parameters[3].Value = strwhere;
            return DbHelperSQL.RunProcedure("MonthGoodsExpense", parameters, "#GoodsData");
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select OrderID,OrderAccount,OrderType,OrderMemID,OrderTotalMoney,OrderDiscountMoney,OrderPayCard,OrderPayCash,OrderPayCoupon,OrderPoint,OrderRemark,OrderPayType,OrderShopID,OrderCreateTime,OrderUserID,OrderIsCard,OrderIsCash,OrderIsBink,OrderPayBink,OldAccount,OrderCardBalance,OrderCardPoint ");
            builder.Append(" FROM OrderLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" OrderID,OrderAccount,OrderType,OrderMemID,OrderTotalMoney,OrderDiscountMoney,OrderPayCard,OrderPayCash,OrderPoint,OrderRemark,OrderPayType,OrderShopID,OrderCreateTime,OrderUserID,OrderIsCard,OrderIsCash,OrderIsBink,OrderPayBink,OrderPayCoupon,OldAccount,OrderCardBalance,OrderCardPoint ");
            builder.Append(" FROM OrderLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.OrderID desc");
            }
            builder.Append(")AS Row, T.*  from OrderLog T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(int PageSize, int PageIndex, out int resCount, params string[] strWhere)
        {
            string tableName = "OrderLog,Mem,SysShop,SysUser";
            string[] column = new string[] { "OrderLog.*,Mem.MemName,Mem.MemCard,Mem.MemID,Mem.MemLevelID,SysShop.ShopName,SysUser.UserName,(select count(orderdetailid) from orderdetail where orderdetail.orderid= orderlog.orderid ) as Count" };
            string[] condition = strWhere;
            int recordCount = 1;
            DataSet set = DbHelperSQL.GetTable(tableName, column, condition, "OrderCreateTime", "OrderID", false, PageSize, PageIndex, recordCount);
            resCount = int.Parse(set.Tables[1].Rows[0][0].ToString());
            return set;
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("OrderID", "OrderLog");
        }

        public DataSet GetMemExpenseMoney(string strSql)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select isnull(count( distinct OrderMemID ),0) as TotalNumber,isnull(sum(OrderDiscountMoney),0) as TotalMoney from OrderLog,Mem ");
            if (strSql.Trim() != "")
            {
                builder.Append(" where " + strSql);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public Chain.Model.OrderLog GetModel(int OrderID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 OrderID,OrderAccount,OrderType,OrderMemID,OrderTotalMoney,OrderDiscountMoney,OrderPayCard,OrderPayCash,OrderPayCoupon,OrderPoint,OrderRemark,OrderPayType,OrderShopID,OrderCreateTime,OrderUserID,OrderIsCard,OrderIsCash,OrderIsBink,OrderPayBink,OldAccount,OrderCardBalance,OrderCardPoint from OrderLog ");
            builder.Append(" where OrderID=@OrderID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderID", SqlDbType.Int, 4) };
            cmdParms[0].Value = OrderID;
            Chain.Model.OrderLog log = new Chain.Model.OrderLog();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["OrderID"] != null) && (set.Tables[0].Rows[0]["OrderID"].ToString() != ""))
            {
                log.OrderID = int.Parse(set.Tables[0].Rows[0]["OrderID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderAccount"] != null) && (set.Tables[0].Rows[0]["OrderAccount"].ToString() != ""))
            {
                log.OrderAccount = set.Tables[0].Rows[0]["OrderAccount"].ToString();
            }
            if ((set.Tables[0].Rows[0]["OrderType"] != null) && (set.Tables[0].Rows[0]["OrderType"].ToString() != ""))
            {
                log.OrderType = int.Parse(set.Tables[0].Rows[0]["OrderType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderMemID"] != null) && (set.Tables[0].Rows[0]["OrderMemID"].ToString() != ""))
            {
                log.OrderMemID = int.Parse(set.Tables[0].Rows[0]["OrderMemID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderTotalMoney"] != null) && (set.Tables[0].Rows[0]["OrderTotalMoney"].ToString() != ""))
            {
                log.OrderTotalMoney = decimal.Parse(set.Tables[0].Rows[0]["OrderTotalMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderDiscountMoney"] != null) && (set.Tables[0].Rows[0]["OrderDiscountMoney"].ToString() != ""))
            {
                log.OrderDiscountMoney = decimal.Parse(set.Tables[0].Rows[0]["OrderDiscountMoney"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderPayCard"] != null) && (set.Tables[0].Rows[0]["OrderPayCard"].ToString() != ""))
            {
                log.OrderPayCard = decimal.Parse(set.Tables[0].Rows[0]["OrderPayCard"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderPayCash"] != null) && (set.Tables[0].Rows[0]["OrderPayCash"].ToString() != ""))
            {
                log.OrderPayCash = decimal.Parse(set.Tables[0].Rows[0]["OrderPayCash"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderPayCoupon"] != null) && (set.Tables[0].Rows[0]["OrderPayCoupon"].ToString() != ""))
            {
                log.OrderPayCoupon = decimal.Parse(set.Tables[0].Rows[0]["OrderPayCoupon"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderPoint"] != null) && (set.Tables[0].Rows[0]["OrderPoint"].ToString() != ""))
            {
                log.OrderPoint = int.Parse(set.Tables[0].Rows[0]["OrderPoint"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderRemark"] != null) && (set.Tables[0].Rows[0]["OrderRemark"].ToString() != ""))
            {
                log.OrderRemark = set.Tables[0].Rows[0]["OrderRemark"].ToString();
            }
            if ((set.Tables[0].Rows[0]["OrderPayType"] != null) && (set.Tables[0].Rows[0]["OrderPayType"].ToString() != ""))
            {
                log.OrderPayType = int.Parse(set.Tables[0].Rows[0]["OrderPayType"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderShopID"] != null) && (set.Tables[0].Rows[0]["OrderShopID"].ToString() != ""))
            {
                log.OrderShopID = int.Parse(set.Tables[0].Rows[0]["OrderShopID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderCreateTime"] != null) && (set.Tables[0].Rows[0]["OrderCreateTime"].ToString() != ""))
            {
                log.OrderCreateTime = DateTime.Parse(set.Tables[0].Rows[0]["OrderCreateTime"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderUserID"] != null) && (set.Tables[0].Rows[0]["OrderUserID"].ToString() != ""))
            {
                log.OrderUserID = int.Parse(set.Tables[0].Rows[0]["OrderUserID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderIsCard"] != null) && (set.Tables[0].Rows[0]["OrderIsCard"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["OrderIsCard"].ToString() == "1") || (set.Tables[0].Rows[0]["OrderIsCard"].ToString().ToLower() == "true"))
                {
                    log.OrderIsCard = true;
                }
                else
                {
                    log.OrderIsCard = false;
                }
            }
            if ((set.Tables[0].Rows[0]["OrderIsCash"] != null) && (set.Tables[0].Rows[0]["OrderIsCash"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["OrderIsCash"].ToString() == "1") || (set.Tables[0].Rows[0]["OrderIsCash"].ToString().ToLower() == "true"))
                {
                    log.OrderIsCash = true;
                }
                else
                {
                    log.OrderIsCash = false;
                }
            }
            if ((set.Tables[0].Rows[0]["OrderIsBink"] != null) && (set.Tables[0].Rows[0]["OrderIsBink"].ToString() != ""))
            {
                if ((set.Tables[0].Rows[0]["OrderIsBink"].ToString() == "1") || (set.Tables[0].Rows[0]["OrderIsBink"].ToString().ToLower() == "true"))
                {
                    log.OrderIsBink = true;
                }
                else
                {
                    log.OrderIsBink = false;
                }
            }
            if ((set.Tables[0].Rows[0]["OrderPayBink"] != null) && (set.Tables[0].Rows[0]["OrderPayBink"].ToString() != ""))
            {
                log.OrderPayBink = decimal.Parse(set.Tables[0].Rows[0]["OrderPayBink"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OldAccount"] != null) && (set.Tables[0].Rows[0]["OldAccount"].ToString() != ""))
            {
                log.OldAccount = set.Tables[0].Rows[0]["OldAccount"].ToString();
            }
            if ((set.Tables[0].Rows[0]["OrderCardBalance"] != null) && (set.Tables[0].Rows[0]["OrderCardBalance"].ToString() != ""))
            {
                log.OrderCardBalance = decimal.Parse(set.Tables[0].Rows[0]["OrderCardBalance"].ToString());
            }
            if ((set.Tables[0].Rows[0]["OrderCardPoint"] != null) && (set.Tables[0].Rows[0]["OrderCardPoint"].ToString() != ""))
            {
                log.OrderCardPoint = int.Parse(set.Tables[0].Rows[0]["OrderCardPoint"].ToString());
            }
            return log;
        }

        public DataSet GetOrderByTime(DateTime starttime, DateTime endtime, string strwhere)
        {
            SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@StartTime", SqlDbType.DateTime), new SqlParameter("@EndTime", SqlDbType.DateTime), new SqlParameter("@strWhere", SqlDbType.NVarChar, 0x7d0) };
            parameters[0].Value = starttime;
            parameters[1].Value = endtime;
            parameters[2].Value = strwhere;
            return DbHelperSQL.RunProcedure("MonthOrder", parameters, "#OrderData");
        }

        public int GetPoint(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SUM(OrderPoint) FROM OrderLog,Mem ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM OrderLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public DataSet GetTotal(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select sum(OrderDiscountMoney)as TotalMoney,sum(OrderPoint)as TotalPoint ");
            builder.Append(" FROM OrderLog ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public decimal GetTotalCash(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select sum(OrderPayCash) from OrderLog where OrderType <> 3 ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" and " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0M;
            }
            return Convert.ToDecimal(single);
        }

        public decimal GetTotalMoney(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select SUM(OrderTotalMoney) FROM OrderLog,Mem,MemLevel,SysShop,SysUser ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0M;
            }
            return Convert.ToDecimal(single);
        }

        public int Update(Chain.Model.OrderLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update OrderLog set ");
            builder.Append("OrderAccount=@OrderAccount,");
            builder.Append("OrderType=@OrderType,");
            builder.Append("OrderMemID=@OrderMemID,");
            builder.Append("OrderTotalMoney=@OrderTotalMoney,");
            builder.Append("OrderDiscountMoney=@OrderDiscountMoney,");
            builder.Append("OrderPayCard=@OrderPayCard,");
            builder.Append("OrderPayCash=@OrderPayCash,");
            builder.Append("OrderPoint=@OrderPoint,");
            builder.Append("OrderRemark=@OrderRemark,");
            builder.Append("OrderPayType=@OrderPayType,");
            builder.Append("OrderShopID=@OrderShopID,");
            builder.Append("OrderCreateTime=@OrderCreateTime,");
            builder.Append("OrderUserID=@OrderUserID,");
            builder.Append("OrderIsCard=@OrderIsCard,");
            builder.Append("OrderIsCash=@OrderIsCash,");
            builder.Append("OrderIsBink=@OrderIsBink,");
            builder.Append("OrderPayBink=@OrderPayBink,");
            builder.Append("OldAccount=@OldAccount,");
            builder.Append("OrderCardBalance=@OrderCardBalance,");
            builder.Append("OrderCardPoint=@OrderCardPoint");
            builder.Append(" where OrderID=@OrderID");
            SqlParameter[] cmdParms = new SqlParameter[] { 
                new SqlParameter("@OrderAccount", SqlDbType.VarChar, 50), new SqlParameter("@OrderType", SqlDbType.TinyInt, 1), new SqlParameter("@OrderMemID", SqlDbType.Int, 4), new SqlParameter("@OrderTotalMoney", SqlDbType.Money, 8), new SqlParameter("@OrderDiscountMoney", SqlDbType.Money, 8), new SqlParameter("@OrderPayCard", SqlDbType.Money, 8), new SqlParameter("@OrderPayCash", SqlDbType.Money, 8), new SqlParameter("@OrderPoint", SqlDbType.Int, 4), new SqlParameter("@OrderRemark", SqlDbType.NVarChar, 500), new SqlParameter("@OrderPayType", SqlDbType.TinyInt, 1), new SqlParameter("@OrderShopID", SqlDbType.Int, 4), new SqlParameter("@OrderCreateTime", SqlDbType.DateTime), new SqlParameter("@OrderUserID", SqlDbType.Int, 4), new SqlParameter("@OrderIsCard", SqlDbType.Bit), new SqlParameter("@OrderIsCash", SqlDbType.Bit), new SqlParameter("@OrderIsBink", SqlDbType.Bit), 
                new SqlParameter("@OrderPayBink", SqlDbType.Money, 8), new SqlParameter("@OldAccount", SqlDbType.NVarChar, 50), new SqlParameter("@OrderCardBalance", SqlDbType.Money, 8), new SqlParameter("@OrderCardPoint", SqlDbType.Int, 4), new SqlParameter("@OrderID", SqlDbType.Int, 4)
             };
            cmdParms[0].Value = model.OrderAccount;
            cmdParms[1].Value = model.OrderType;
            cmdParms[2].Value = model.OrderMemID;
            cmdParms[3].Value = model.OrderTotalMoney;
            cmdParms[4].Value = model.OrderDiscountMoney;
            cmdParms[5].Value = model.OrderPayCard;
            cmdParms[6].Value = model.OrderPayCash;
            cmdParms[7].Value = model.OrderPoint;
            cmdParms[8].Value = model.OrderRemark;
            cmdParms[9].Value = model.OrderPayType;
            cmdParms[10].Value = model.OrderShopID;
            cmdParms[11].Value = model.OrderCreateTime;
            cmdParms[12].Value = model.OrderUserID;
            cmdParms[13].Value = model.OrderIsCard;
            cmdParms[14].Value = model.OrderIsCash;
            cmdParms[15].Value = model.OrderIsBink;
            cmdParms[0x10].Value = model.OrderPayBink;
            cmdParms[0x11].Value = model.OldAccount;
            cmdParms[0x12].Value = model.OrderCardBalance;
            cmdParms[0x13].Value = model.OrderCardPoint;
            cmdParms[20].Value = model.OrderID;
            int num = DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
            if (num > 0)
            {
                return num;
            }
            return 0;
        }

        public int UpdateLog(int orderID, decimal totalMoney, decimal discountMoney, int point, decimal payCard, decimal payCash, decimal payBank, string oldAccount)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" update OrderLog set OrderTotalMoney =@OrderTotalMoney,");
            builder.Append(" OrderDiscountMoney=@OrderDiscountMoney,");
            builder.Append(" OrderPoint=@OrderPoint,");
            builder.Append(" OrderPayCard=@OrderPayCard,");
            builder.Append(" OrderPayCash=@OrderPayCash,");
            builder.Append(" OrderPayBink=@OrderPayBink,");
            builder.Append(" OldAccount=@OldAccount");
            builder.Append(" where OrderID=@OrderID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderTotalMoney", SqlDbType.Money, 8), new SqlParameter("@OrderDiscountMoney", SqlDbType.Money, 8), new SqlParameter("@OrderPoint", SqlDbType.Int, 8), new SqlParameter("@OrderPayCard", SqlDbType.Money, 8), new SqlParameter("@OrderPayCash", SqlDbType.Money, 8), new SqlParameter("@OrderPayBink", SqlDbType.Money, 8), new SqlParameter("@OldAccount", SqlDbType.VarChar, 50), new SqlParameter("@OrderID", SqlDbType.Int, 4) };
            cmdParms[0].Value = totalMoney;
            cmdParms[1].Value = discountMoney;
            cmdParms[2].Value = point;
            cmdParms[3].Value = payCard;
            cmdParms[4].Value = payCash;
            cmdParms[5].Value = payBank;
            cmdParms[6].Value = oldAccount;
            cmdParms[7].Value = orderID;
            return DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
        }

        public int UpdateOrderLog(Chain.Model.OrderLog model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" update OrderLog set ");
            builder.Append(" OrderPayType=@OrderPayType,");
            builder.Append(" OrderType=@OrderType,");
            builder.Append(" OrderDiscountMoney=@OrderDiscountMoney,");
            builder.Append(" OrderPayCard=@OrderPayCard,");
            builder.Append(" OrderPayCash=@OrderPayCash,");
            builder.Append(" OrderPayBink=@OrderPayBink,");
            builder.Append(" OrderCreateTime=@OrderCreateTime");
            builder.Append(" where OrderID=@OrderID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@OrderPayType", SqlDbType.TinyInt, 1), new SqlParameter("@OrderType", SqlDbType.TinyInt, 1), new SqlParameter("@OrderDiscountMoney", SqlDbType.Money, 8), new SqlParameter("@OrderPayCard", SqlDbType.Money, 8), new SqlParameter("@OrderPayCash", SqlDbType.Money, 8), new SqlParameter("@OrderPayBink", SqlDbType.Money, 8), new SqlParameter("@OrderCreateTime", SqlDbType.DateTime), new SqlParameter("@OrderID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.OrderPayType;
            cmdParms[1].Value = model.OrderType;
            cmdParms[2].Value = model.OrderDiscountMoney;
            cmdParms[3].Value = model.OrderPayCard;
            cmdParms[4].Value = model.OrderPayCash;
            cmdParms[5].Value = model.OrderPayBink;
            cmdParms[6].Value = model.OrderCreateTime;
            cmdParms[7].Value = model.OrderID;
            object obj2 = DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms);
            if (obj2 == null)
            {
                return 0;
            }
            return Convert.ToInt32(obj2);
        }

        public int UpdateType(int orderID, int type)
        {
            return DbHelperSQL.ExecuteSql(string.Concat(new object[] { " update OrderLog set OrderType=", type, " where OrderID=", orderID }));
        }
    }
}

